package org.angelica.modules.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.angelica.core.query.BaseQuery;
import org.angelica.modules.account.dao.AccPermissionDao;
import org.angelica.modules.account.entity.AccPermissionDO;
import org.angelica.modules.account.service.AccPermissionService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author aizhimin
 * @since 2019-05-13
 */
@Service
public class AccPermissionServiceImpl extends ServiceImpl<AccPermissionDao, AccPermissionDO> implements AccPermissionService {

    @Override
    public IPage<AccPermissionDO> listPermission(BaseQuery baseQuery) {
        IPage<AccPermissionDO> adminPermissionPage = this.page(baseQuery.buildPage(),new QueryWrapper<AccPermissionDO>()
                .eq("parent_id",0)
                .orderByDesc("priority"));

        List<AccPermissionDO> rootList = adminPermissionPage.getRecords();
        for(AccPermissionDO adminPermissionDO:rootList){
            adminPermissionDO.setChildren(listChildren(adminPermissionDO));
            //填充treeselect组件所需数据
            adminPermissionDO.setKey(String.valueOf(adminPermissionDO.getId()));
            adminPermissionDO.setTitle(adminPermissionDO.getPermName());
            adminPermissionDO.setValue(String.valueOf(adminPermissionDO.getId()));
        }
        return adminPermissionPage;
    }

    @Override
    public List<AccPermissionDO> listTreeData() {
        //查询所有根节点
        List<AccPermissionDO> rootList = this.list(new QueryWrapper<AccPermissionDO>()
                .eq("parent_id",0)
                .orderByDesc("priority"));

        for(AccPermissionDO adminPermissionDO:rootList){
            List<AccPermissionDO> children = listChildren(adminPermissionDO);
            if(!children.isEmpty()){
                adminPermissionDO.setChildren(children);
            }

            //填充treeselect组件所需数据
            adminPermissionDO.setKey(String.valueOf(adminPermissionDO.getId()));
            adminPermissionDO.setTitle(adminPermissionDO.getPermName());
            adminPermissionDO.setValue(String.valueOf(adminPermissionDO.getId()));
        }
        //设置根权限
        rootList.add(AccPermissionDO.builder().key("0").title("根权限").value("0").build());
        return rootList;
    }

    /**
     * 递归查询子节点
     * @param adminPermissionDO
     * @return
     */
    private List<AccPermissionDO> listChildren(AccPermissionDO adminPermissionDO){
        List<AccPermissionDO> permList =  this.list(new QueryWrapper<AccPermissionDO>()
                .eq("parent_id",adminPermissionDO.getId())
                .orderByDesc("priority"));

        if(!permList.isEmpty()){
            //设置子节点
            adminPermissionDO.setChildren(permList);
        }else{
            return Lists.newArrayList();
        }
        for(AccPermissionDO adminPermission:permList){
            listChildren(adminPermission);
            //填充treeselect组件所需数据
            adminPermission.setKey(String.valueOf(adminPermission.getId()));
            adminPermission.setTitle(adminPermission.getPermName());
            adminPermission.setValue(String.valueOf(adminPermission.getId()));
        }
        return permList;
    }

}
